'''
 * @Coding: UTF-8
 * @Author: GDNDZZK
 * @Date: 2023-04-02 16:09:47
 * @LastEditTime: 2023-04-02 18:05:55
 * @LastEditors: GDNDZZK
'''


def read_ini(filename='./conf/config.ini') -> dict:
    # !(): 这行注释用于修复vsc中的高亮错误,不影响代码运行
    # 创建一个空字典
    result = {}
    # 打开文件
    with open(filename, "r", encoding='utf-8') as f:
        # 遍历文件的每一行
        for line in f:
            # 去掉行尾的换行符
            line = line.strip()
            # 如果行不为空，且不以;开头
            if line and not line.startswith(";"):
                # 用等号分割键和值
                key, value = line.split("=", 1)
                # 将键值对添加到字典中
                result[key] = value
    # 返回字典
    return result


'''
 * @Coding: UTF-8
 * @Author: 712f
 * @Date: 2023-06-28 23:40:36
 * @LastEditTime: 2023-06-28 23:40:36
 * @LastEditors: 712f
'''


class remove_config():
    def __init__(self, user, passwd):
        self.user = user
        self.passwd = passwd

    def gitRun(self):
        from git import Repo
        import shutil
        import os
        import stat
        # 创建一个Repo对象，指定用户名和密码
        repo = Repo.clone_from(f"https://{self.user}:{self.passwd}@gitee.com/ywt712/glm-request-url.git",
                               "temp_repo",  # 临时目录
                               branch="master",  # 分支名
                               depth=1)  # 只克隆最近的一次提交

        # 复制
        shutil.copy("temp_repo/config.ini", "conf/config.ini")

        # 更改权限
        def make_dir_writable(function, path, exception):
            # 如果删除失败，尝试修改权限并重试
            os.chmod(path, stat.S_IWRITE)
            function(path)

        path = "temp_repo"  # 待删除的目录
        try:
            shutil.rmtree(path, onerror=make_dir_writable)
        except OSError as e:
            print("error: {0}; path: {1}".format(path, e.strerror))
